package t682

import (
	"errors"
	"sync"
)

// MyStack type
type MyStack struct {
	Items []int
	lock  sync.RWMutex
}

// RPush is func
func (m *MyStack) Push(x int) {
	m.lock.Lock()
	defer m.lock.Unlock()
	m.Items = append(m.Items, x)
}

// LPop is func
func (m *MyStack) Pop() (int, error) {
	m.lock.Lock()
	defer m.lock.Unlock()
	if len(m.Items) == 0 {
		return 0, errors.New("errors new")
	}
	pop := m.Items[len(m.Items)-1]
	m.Items = m.Items[:len(m.Items)-1]
	return pop, nil
}

// LTop is func
func (m *MyStack) Top() (int, error) {
	m.lock.Lock()
	defer m.lock.Unlock()
	if len(m.Items) == 0 {
		return 0, errors.New("errors new")
	}
	return m.Items[len(m.Items)-1], nil
}

// Clear is func
func (m *MyStack) Clear() {
	m.lock.Lock()
	defer m.lock.Unlock()
	m.Items = make([]int, 0)
}
